﻿// -----------------------------------------------------------------------
// <copyright file="StudentRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.Data.Linq;
    using AGR.Model;

    /// <summary>
    /// Student data access
    /// </summary>
    public class StudentRepository : AbstractRepository<Student>
    {
        public StudentRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<Student> GetAll()
        {
            var result = (from x in base._context.Students
                          select new Student
                          {
                              Id = x.Id,
                              StudentCode = x.StudentCode,
                              FirstName = x.FirstName,
                              LastName = x.LastName,
                              Gender = x.Gender,
                              IdNumber = x.IdNumber,
                              Contact = x.Contact,
                              FirstLanguage = x.FirstLanguage,
                              SecondLanguage = x.SecondLanguage,
                              ResidentialStatus = x.ResidentialStatus,
                              PhysicalAddress = x.PhysicalAddress,
                              PostalAddress = x.PostalAddress,
                              Nationality = x.Nationality,
                              Province = x.Province,
                              Equity = x.Equity,
                              Disability = x.Disability,
                              SocioStatus = x.SocioStatus,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Student entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Student entityToAdd = new Context.Student
            {
                Id = entity.Id,
                StudentCode = entity.StudentCode,
                FirstName = entity.FirstName,
                LastName = entity.LastName,
                Gender = entity.Gender,
                IdNumber = entity.IdNumber,
                Contact = entity.Contact,
                FirstLanguage = entity.FirstLanguage,
                SecondLanguage = entity.SecondLanguage,
                ResidentialStatus = entity.ResidentialStatus,
                PhysicalAddress = entity.PhysicalAddress,
                PostalAddress = entity.PostalAddress,
                Nationality = entity.Nationality,
                Province = entity.Province,
                Equity = entity.Equity,
                Disability = entity.Disability,
                SocioStatus = entity.SocioStatus,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };
           
            base._context.Students.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Student entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Student entityToUpdate = base._context.Students.Single(x => x.StudentCode.Equals(entity.StudentCode));

            entityToUpdate.Id = entity.Id;
            entityToUpdate.StudentCode = entity.StudentCode;
            entityToUpdate.FirstName = entity.FirstName;
            entityToUpdate.LastName = entity.LastName;
            entityToUpdate.Gender = entity.Gender;
            entityToUpdate.IdNumber = entity.IdNumber;
            entityToUpdate.Contact = entity.Contact;
            entityToUpdate.FirstLanguage = entity.FirstLanguage;
            entityToUpdate.SecondLanguage = entity.SecondLanguage;
            entityToUpdate.ResidentialStatus = entity.ResidentialStatus;
            entityToUpdate.PhysicalAddress = entity.PhysicalAddress;
            entityToUpdate.PostalAddress = entity.PostalAddress;
            entityToUpdate.Nationality = entity.Nationality;
            entityToUpdate.Province = entity.Province;
            entityToUpdate.Equity = entity.Equity;
            entityToUpdate.Disability = entity.Disability;
            entityToUpdate.SocioStatus = entity.SocioStatus;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Student entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Student entityToDelete = base._context.Students.Single(x => x.StudentCode.Equals(entity.StudentCode));

            base._context.Students.DeleteOnSubmit(entityToDelete);
        }
    }
}
